package com.wgts.wgts.control.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.wgts.wgts.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/*
 *
 *   data:2018/9/1111:04
 *
 */public class CancelOrderDialogFragment extends AbstractCustomDialogFragment {
    @BindView(R.id.order_again_ly)
    LinearLayout orderAgainLy;
    @BindView(R.id.un_wished_ly)
    LinearLayout unWishedLy;
    @BindView(R.id.point_error_ly)
    LinearLayout pointErrorLy;
    Unbinder unbinder;
    private View view;
    private static final String cancelDialogBundle = "cancelDialogBundle";
    private FragmentManager fragmentManager;
    private CancelDialogData dialogData;

    @Override
    public void onStart() {
        super.onStart();
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

    @Override
    public View onCreateDialogView() {
        Bundle bundle = getArguments();
        if (bundle != null){
            dialogData = bundle.getParcelable(cancelDialogBundle);
            fragmentManager = dialogData.getFragmentManager();
        }
        view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_fragment_cancel_order, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.order_again_ly, R.id.un_wished_ly, R.id.point_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.order_again_ly:
                /*ToastDate date = new ToastDate();
                date.setCancelQuestion("确定要取消订单重新下单吗？");
                date.setCancelText("取消订单");
                date.setPointText("点错了");
                date.setFragmentManager(fragmentManager);
                ToastDialogFragment fragment = ToastDialogFragment.setToastMessage(date);
                fragment.show(date.getFragmentManager(), "cancelToast");*/
                dismiss();
                break;
            case R.id.un_wished_ly:
                ToastDate toastDate = new ToastDate();
                toastDate.setCancelQuestion("确定要取消订单吗？");
                toastDate.setCancelText("取消订单");
                toastDate.setPointText("点错了");
                toastDate.setFragmentManager(fragmentManager);
                ToastDialogFragment dialogFragment = ToastDialogFragment.setToastMessage(toastDate);
                dialogFragment.show(toastDate.getFragmentManager(), "UnWishedToast");
                dismiss();
                break;
            case R.id.point_error_ly:
                dismiss();
                break;
        }
    }

    public static CancelOrderDialogFragment getCancelDialogFragment(CancelDialogData data){
        CancelOrderDialogFragment dialogFragment = new CancelOrderDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(cancelDialogBundle, data);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }
}
